//
//  EditProduceViewController.m
//  OperationPro
//
//  Created by yaxue wang on 2017/2/6.
//  Copyright © 2017年 展鸿科技. All rights reserved.
//

#import "EditProduceViewController.h"
#import "EditPhonePollingViewController.h"
#import "PollingDetailViewController.h"
@interface EditProduceViewController ()

@end

@implementation EditProduceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self setTitle:@"生成报告"];
    __block typeof(self) blockself = self;
    [self.navBar setBackAction:^{
        
        UINavigationController *navcontroller = blockself.navigationController;
        NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:navcontroller.viewControllers];
        
        for (UIViewController *temp in viewControllers)
        {
            if ([temp isKindOfClass:[PollingDetailViewController class]])
            {
                [blockself.navigationController popToViewController:temp animated:YES];
            }
        }
    }];
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(27, 64, WindowWidth-54, WindowHeight-64)];
    bgView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:bgView];
   
    UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, bgView.bounds.size.width, 30)];
    [bgView addSubview:lblTitle];
    lblTitle.text=@"报告将发送给以下联系人进行确认：";
    lblTitle.textColor=C(@"212121");
    [lblTitle setFont:F(14)];
    CGFloat height;

    //是否有联系人 0是没有 1是有
    if ([self.isHasPhone isEqualToString:@"0"]) {
        NSDictionary *dic=[self.mobileAr objectAtIndex:0];
        UILabel *phoneLab=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(lblTitle.frame)+5, bgView.bounds.size.width-15-60, 30)];
        phoneLab.text=[NSString stringWithFormat:@"%@ %@",[dic objectForKey:@"name"],[dic objectForKey:@"mobile"]];
        phoneLab.textColor=MainColor;
        [phoneLab setFont:F(14)];
        [bgView addSubview:phoneLab];
        UILabel *editLab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(phoneLab.frame), CGRectGetMaxY(lblTitle.frame)+5,60, 30)];
        editLab.text=@"修改";
        editLab.textColor=MainColor;
        [editLab setFont:F(14)];
        [bgView addSubview:editLab];
        editLab.userInteractionEnabled=YES;
        
        [editLab bk_whenTapped:^{
            //修改
            EditPhonePollingViewController*editVC=[[EditPhonePollingViewController alloc]init];
            NSString*string =[dic objectForKey:@"mobile"];
            NSString*string1 = [string substringWithRange:NSMakeRange(0, 3)];
            NSString*string2 = [string substringWithRange:NSMakeRange(3, 4)];
            NSString*string3 = [string substringWithRange:NSMakeRange(7, 4)];
            editVC.phoneString=[NSString stringWithFormat:@"%@ %@ %@",string1,string2,string3];
            
            editVC.Uuid=self.Uuid;
            editVC.name=[dic objectForKey:@"name"];
            editVC.isHasPhone=self.isHasPhone;
            editVC.mobileAr=self.mobileAr;
            editVC.index=0;
            [self pushController:editVC];
            
        
        }];
        
        UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
        [bgView addSubview:button];
        button.frame=CGRectMake(0,CGRectGetMaxY(editLab.frame)+20, bgView.bounds.size.width, 40);
        [button setBackgroundColor:MainColor];
        button.layer.masksToBounds=YES;
        button.layer.cornerRadius=8;
        [button setTitle:@"发送" forState:UIControlStateNormal];
        [button bk_addEventHandler:^(id sender) {
            //发送生成报告请求
   
            
            NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
            [dict setObject:self.Uuid forKey:@"inspection_uuid"];
            [dict setObject:self.mobileAr forKey:@"contact"];
            
            
            
            NSData *data=[NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
            NSString *jsonString=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            
            //生成报告请求
            [CClass HttpRequestTask:YES withUrl:@"inspection_createReport" withData:@{@"data":jsonString} notifyError:YES onSuccess:^(BaseModel *model) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"createPolling" object:nil];
                UINavigationController *navcontroller = self.navigationController;
                NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:navcontroller.viewControllers];
                
                for (UIViewController *temp in viewControllers)
                {
                    if ([temp isKindOfClass:[PollingDetailViewController class]])
                    {
                        [self.navigationController popToViewController:temp animated:YES];
                    }
                }
                
            } onError:^(BaseModel *model) {
                
            }];
            
        } forControlEvents:UIControlEventTouchUpInside];

    }else
    {
        for (int i=0; i<self.mobileAr.count; i++) {
            NSDictionary *dic=[self.mobileAr objectAtIndex:i];
            NSString *context=[NSString stringWithFormat:@"%@ %@",[dic objectForKey:@"name"],[dic objectForKey:@"mobile"]];
            UILabel *phoneLab=[[UILabel alloc]init];
            phoneLab.textColor=MainColor;
            [phoneLab setFont:F(14)];
            phoneLab.text=context;
            [bgView addSubview:phoneLab];
            if (i>0) {
            phoneLab.frame=CGRectMake(0, CGRectGetMaxY(lblTitle.frame)+5+30*i, bgView.bounds.size.width-15-60, 30);
                
            UILabel *editLab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(phoneLab.frame), CGRectGetMaxY(lblTitle.frame)+5+30*i,60, 30)];
            editLab.text=@"修改";
            editLab.textColor=MainColor;
            [editLab setFont:F(14)];
            [bgView addSubview:editLab];
            editLab.userInteractionEnabled=YES;
            [editLab bk_whenTapped:^{
                //修改
                EditPhonePollingViewController*editVC=[[EditPhonePollingViewController alloc]init];
                NSString*string =[dic objectForKey:@"mobile"];
                NSString*string1 = [string substringWithRange:NSMakeRange(0, 3)];
                NSString*string2 = [string substringWithRange:NSMakeRange(3, 4)];
                NSString*string3 = [string substringWithRange:NSMakeRange(7, 4)];
                editVC.phoneString=[NSString stringWithFormat:@"%@ %@ %@",string1,string2,string3];
                
                editVC.Uuid=self.Uuid;
                editVC.name=[dic objectForKey:@"name"];
                editVC.isHasPhone=self.isHasPhone;
                editVC.mobileAr=self.mobileAr;
                editVC.index=i;
                [self pushController:editVC];
            }];
                if (self.isHiddleEdit==YES) {
                    editLab.hidden=YES;
                }else
                {
                    editLab.hidden=NO;

                }
            }else
            {
                phoneLab.frame=CGRectMake(0, CGRectGetMaxY(lblTitle.frame)+5+30*i, bgView.bounds.size.width, 30);

            }
        }
        height=CGRectGetMaxY(lblTitle.frame)+5+30*self.mobileAr.count;

        UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
        [bgView addSubview:button];
        button.frame=CGRectMake(0,height+20, bgView.bounds.size.width, 40);
        [button setBackgroundColor:MainColor];
        button.layer.masksToBounds=YES;
        button.layer.cornerRadius=8;
        [button setTitle:@"发送" forState:UIControlStateNormal];
        [button bk_addEventHandler:^(id sender) {
            //发送生成报告请求
            NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
            [dict setObject:self.Uuid forKey:@"inspection_uuid"];
            [dict setObject:self.mobileAr forKey:@"contact"];
            
    
            NSData *data=[NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
            NSString *jsonString=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            
            //生成报告请求
            [CClass HttpRequestTask:YES withUrl:@"inspection_createReport" withData:@{@"data":jsonString} notifyError:YES onSuccess:^(BaseModel *model) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"createPolling" object:nil];
                UINavigationController *navcontroller = self.navigationController;
                NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:navcontroller.viewControllers];
                
                for (UIViewController *temp in viewControllers)
                {
                    if ([temp isKindOfClass:[PollingDetailViewController class]])
                    {
                        [self.navigationController popToViewController:temp animated:YES];
                    }
                }
                
            } onError:^(BaseModel *model) {
                
            }];
            
        } forControlEvents:UIControlEventTouchUpInside];
 
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
